#!/usr/bin/env bash

SHARD_NUM=0

FROM_PET_DBHOST=
FROM_PET_DBPORT=
FROM_PET_DBUSER=
FROM_PET_DBPASS=
FROM_PET_DBNAME=


TO_PET_DBHOST=
TO_PET_DBPORT=
TO_PET_DBUSER=
TO_PET_DBPASS=
TO_PET_DBNAME=


BATCH_SIZE=1000

CURRENT_TIME=$(date +%Y-%m-%d_%H%M)
mkdir $CURRENT_TIME
cd $CURRENT_TIME
NODATA_FILE=nodata.sql

function syncEachTable(){
    table_name=$1
    if [[ "$TARGET_TABLE"x == x ]] && ([[ "$table_name"x == T_ACCG_ENTRY* ]] || [[ "$table_name"x == T_ACCG_REQ* ]]); then
        return
    fi

    eval FROM_DBHOST=\$FROM_$2_DBHOST
    eval FROM_DBPORT=\$FROM_$2_DBPORT
    eval FROM_DBUSER=\$FROM_$2_DBUSER
    eval FROM_DBPASS=\$FROM_$2_DBPASS
    eval FROM_DBNAME=\$FROM_$2_DBNAME

    eval TO_DBHOST=\$TO_$2_DBHOST
    eval TO_DBPORT=\$TO_$2_DBPORT
    eval TO_DBUSER=\$TO_$2_DBUSER
    eval TO_DBPASS=\$TO_$2_DBPASS
    eval TO_DBNAME=\$TO_$2_DBNAME

    maxid=`mysql -h$FROM_DBHOST -P$FROM_DBPORT -u$FROM_DBUSER -p$FROM_DBPASS $FROM_DBNAME -e "select MAX(ID) rownum from $table_name" | grep -v "rownum"`
    table_size=`mysql -h$FROM_DBHOST -P$FROM_DBPORT -u$FROM_DBUSER -p$FROM_DBPASS $FROM_DBNAME -e "select count(*) size from $table_name" | grep -v "size"`
    echo $table_name, "maxid:"$maxid, "tablesize:"$table_size
    start=0
    for (( i=0; start<=maxid&&maxid>0; i++ ));
    do

        end=$(($start+$BATCH_SIZE))
        echo $start,$end

        mysqldump -h$FROM_DBHOST -P$FROM_DBPORT -u$FROM_DBUSER -p$FROM_DBPASS --skip-lock-tables -nt --skip-opt --quick --extended-insert $FROM_DBNAME $table_name --where="ID>=$start and ID<=$end" > ${table_name}_$i.sql
        mysql -h$TO_DBHOST -P$TO_DBPORT -u$TO_DBUSER -p$TO_DBPASS $TO_DBNAME < ${table_name}_$i.sql

        # 为避免id中间的gap，搜索下一个id
        start=`mysql -h$FROM_DBHOST -P$FROM_DBPORT -u$FROM_DBUSER -p$FROM_DBPASS $FROM_DBNAME -e "select id from $table_name where id>$end order by id asc limit 1" | grep -v "id"`
        if [[ -z ${start} ]];then
            break
        fi

    done
}

function dumpDbStructure(){
# dump db structure
mysqldump -h$FROM_PET_DBHOST -P$FROM_PET_DBPORT -u$FROM_PET_DBUSER -p$FROM_PET_DBPASS --skip-lock-tables -d $FROM_PET_DBNAME $FROM_DBTABLE > $NODATA_FILE
sed -i 's/encrypt//g' $NODATA_FILE
}

function syncNoData(){
# sync db structure
mysql -h$TO_PET_DBHOST -P$TO_PET_DBPORT -u$TO_PET_DBUSER -p$TO_PET_DBPASS $TO_PET_DBNAME < $NODATA_FILE
}

function syncAllOnlyData(){
# find tablename and filter, get 0_$SHARD_NUM and others
sed -nr "/DROP TABLE IF EXISTS/p" $NODATA_FILE | sed -r "s/.*\`([^\`]*)\`;/\1/g" | sed -nr "/(.*0_$SHARD_NUM)|(.*[^0-9]$)/p" | 
while read -r line; do
    echo $line
    sleep 0.2
    syncEachTable $line PET
done
}

type="$1"
if [[ "$type" == "syncAllNoData" ]]; then
    dumpDbStructure
    syncNoData
elif [[ "$type" == "syncAllOnlyData" ]]; then
    dumpDbStructure
    syncAllOnlyData
elif [[ "$type" == "syncAllWithData" ]]; then
    dumpDbStructure
    syncNoData
    syncAllOnlyData
elif [[ "$type" == "syncTableNoData" ]]; then
    FROM_DBTABLE=$2
    syncNoData
elif [[ "$type" == "syncTableOnlyData" ]]; then
    syncEachTable $2 PET
elif [[ "$type" == "syncTableWithData" ]]; then
    FROM_DBTABLE=$2
    syncNoData
    syncEachTable $2 PET
elif [[ "$type" == "restoreDatabase" ]]; then
    mysql -h$TO_PET_DBHOST -P$TO_PET_DBPORT -u$TO_PET_DBUSER -p$TO_PET_DBPASS $TO_PET_DBNAME -e "drop database $TO_PET_DBNAME"
    mysql -h$TO_PET_DBHOST -P$TO_PET_DBPORT -u$TO_PET_DBUSER -p$TO_PET_DBPASS -e "create database $TO_PET_DBNAME"
else
    echo "COMMANDS: "
    echo "      syncAllNoData                     sync all tables structure"
    echo "      syncAllOnlyData                   sync all tables data"
    echo "      syncAllWithData                   sync all tables structure and sync table data"
    echo "      syncTableNoData tablename         sync target table structure"
    echo "      syncTableOnlyData tablename       sync target table data"
    echo "      syncTableWithData tablename       sync target table structure and sync table data"
    echo "      restoreDatabase                   drop and create target db"
    echo ""
fi


